<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\EmployeeModel;
use app\admin\model\WageCycleModel;
use app\models\WageModel;
use app\services\JsonService;
use app\services\UtilService as Util;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Exception;

class WageController extends AdminBaseController
{
    function index(){
        $date       = date('Y-m');
        $count      = WageCycleModel::getCycleCount($date);
        $this->assign('cycle_count',$count);
        return $this->fetch();
    }

    //获取工资数据
    public function wage_data()
    {
        $page   = $this->request->param('page','1');
        $limit  = $this->request->param('limit','10');
        $date   = $this->request->param('date',date('Y-m'));
        $cycle_id = WageCycleModel::getCycleId($date);
        if($cycle_id==null){//本期工资表尚未生成
            JsonService::successlayui(0,[],'本期工资表尚未生成');
        }
        $model  = new WageModel();
        $res    = $model->getListById($page,$limit,$cycle_id);
        $count  = $model->count('id');
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(WageModel::getErrorInfo());
        }
    }


    public function pay()
    {
        $this->assign('info',$this->request->param());
        return $this->fetch();
    }

    public function do_pay()
    {
        $wage_id    = $this->request->param('wage_id');
        $amount     = $this->request->param('amount');
        $model      = new WageModel();
        $re         = $model->wageOperate($wage_id,$amount);
        if($re){
            JsonService::success('操作成功！本次发放¥'.$amount);
        }else{
            JsonService::fail(WageModel::getErrorInfo());
        }
    }

    public function wage_create()
    {
        $date       = date('Y-m');
        $count      = WageCycleModel::getCycleCount($date);
        if($count>0){
            JsonService::fail('本期工资已生成');
        }
        $WageCycleModel = new WageCycleModel();
        $WageModel      = new WageModel();
        $EmployeeModel  = new EmployeeModel();
        $data['name'] = $date.'工资';
        $data['date'] = $date;
        $data['status'] = 1;
        $time = date('Y-m-d h:i:s');
        Db::startTrans();
        try{
            $cycle_id = $WageCycleModel->insertGetId($data);
            $list = $EmployeeModel->where('status',1)->field('id,wages_basic')->select();
            $insert_data = [];
            foreach ($list as $k=>$v){
                $insert_data[$k]['employee_id'] = $v['id'];
                $insert_data[$k]['wages_basic'] = $v['wages_basic'];
                $insert_data[$k]['wage'] = $v['wages_basic'];
                $insert_data[$k]['cycle_id']    = $cycle_id;
                $insert_data[$k]['time']        = $time;
            }
            $num = $WageModel->insertAll($insert_data);
            Db::commit();
            JsonService::success("操作成功！本次生成 $num 张工资条！");
        }catch (Exception $exception){
            Db::rollback();
            JsonService::fail($exception->getMessage());
        }

    }

    public function set_value()
    {
        $bonus      = $this->request->param('bonus');
        $deduction  = $this->request->param('deduction');
        $wage_id    = $this->request->param('id');
        $remark    = $this->request->param('remark');
        $model      = new WageModel();
        $re = $model->setValue($wage_id,$bonus,$deduction,$remark);
        if($re){
            JsonService::success("操作成功！");
        }else{
            JsonService::fail(WageModel::getErrorInfo());
        }
    }

    //设置全勤奖
    public function set_full()
    {
        $is_full    = $this->request->param('is_full');
        $wage_id    = $this->request->param('wage_id');
        $model      = new WageModel();
        $re = $model->setFull($wage_id,$is_full);
        if($re){
            JsonService::success("操作成功！");
        }else{
            JsonService::fail(WageModel::getErrorInfo());
        }
    }

    public function wage_add()
    {
        $data = Util::postMore([
            ['id', 0],
            'employee_id',
            'wage',
            'wages_paid',
            'wages_basic',
            'bonus',
            ['is_full',0],
            'full_attendance'
        ]);
        $data['employee_id'] = 1;
        if($data['id']){

        }else{

        }
    }

}
